import { PrismaService } from '@app/prisma';
import { ClassesEntity } from '@app/prisma/classes.entity/classes.entity';
import { TeacherEntity } from '@app/prisma/teacher.entity/teacher.entity';
import { Injectable } from '@nestjs/common';

@Injectable()
export class TeacherService {
    constructor(
        private readonly prisma: PrismaService
    ) { }

    async findOrCreateTeacher(name: string) {
        const teacher = await this.prisma.teacher.findUnique({ where: { name } });
        if (!!teacher) return teacher;
        return await this.prisma.teacher.create({ data: { name } })
    }
    async findOrCreateClasses(name: string) {
        const classes = await this.prisma.classes.findUnique({ where: { name } });
        if (!!classes) return classes;
        return await this.prisma.classes.create({ data: { name } })
    }
    async findOrCreateClassesOnTeacher(classes: ClassesEntity, teacher: TeacherEntity) {
        const find = await this.prisma.teacherOnClasses.findUnique({
            where: {
                teacherId_classesId: {
                    teacherId: teacher.id,
                    classesId: classes.id
                }
            }
        })
        if (!!find) return find;
        return await this.prisma.teacherOnClasses.create({
            data: {
                teacher: {
                    connect: {
                        id: teacher.id
                    }
                },
                classes: {
                    connect: {
                        id: classes.id
                    }
                }
            }
        })
    }
}
